﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VirtualTournaments.Services.Tournaments;
using VirtualTournaments.Services.Bets;
using System.Data.SqlClient;
using System.Data; 

namespace VirtualTournaments.Business.Tests.ServicesTests
{
	/// <summary>
	/// Summary description for TestBetService
	/// </summary>
	[TestClass]
	public class TestBetService
	{
		public TestBetService()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		} 

		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) { }
		
		[ClassCleanup()]
		public static void MyClassCleanup() { }

		Team[] teams;
		SqlConnection connection;

		[TestInitialize()]
		public void MyTestInitialize()
		{
			connection = DataAccess.SqlServer.Connections.ConnectionManager.GetConnection() as SqlConnection;
			connection.Open();
			DatabaseTestHelper.DeleteAll(connection);
			DatabaseTestHelper.InsertTeams(connection);

			teams = new Team[] { new Team("Boca"), new Team("River"), new Team("Vélez"), new Team("Banfield"), new Team("Independiente"), new Team("Racing") };
		}
		
		[TestCleanup()]
		public void MyTestCleanup()
		{
			DatabaseTestHelper.DeleteAll(connection);
			connection.Close();
		}
		//[TestMethod]
		[TestMethod, Ignore]
		public void GetTorunamentNextJourneyMatchOddsTest2()
		{
			Tournament tournament = new Tournament("Apertura 09", Tournament.ALLAGAINSTALL_TYPE);
			Services.Tournaments.TournamentsService.CreateTournament(teams, tournament);
			JourneyMatchOdds[] matchesOdds = Services.Bets.BetsService.GetTorunamentNextJourneyMatchOdds(tournament);
		}

		//[TestMethod]
		[TestMethod, Ignore]
		public void GetTournamentNextJourneyMatchesTest()
		{
			Tournament tournament = new Tournament("Apertura 09", Tournament.ALLAGAINSTALL_TYPE);
			Services.Tournaments.TournamentsService.CreateTournament(teams, tournament);

			tournament = Services.Tournaments.TournamentsService.GetTournamentByName("Apertura 09");
			Match[] matches = Services.Tournaments.TournamentsService.GetTournamentNextJourneyMatches(tournament);

			Assert.AreEqual(3, matches.Count());
			var teamsplaying = (from match in matches
					 select match.Local).Union(from match in matches select match.Visitor);
			var intersection = teamsplaying.Intersect(from team in teams.AsQueryable() select team.Name);

			Assert.AreEqual(teams.Count(), intersection.Count());
			Assert.AreEqual(teamsplaying.Count(), intersection.Count());
		}

		//[TestMethod]
		[TestMethod, Ignore]
		public void TournamentPlayNextJourneyTest2()
		{
			Tournament tournament = new Tournament("Apertura 09", Tournament.ALLAGAINSTALL_TYPE);
			Services.Tournaments.TournamentsService.CreateTournament(teams, tournament);

			tournament = Services.Tournaments.TournamentsService.GetTournamentByName("Apertura 09");
			Match[] firstJourneyMatches = Services.Tournaments.TournamentsService.GetTournamentNextJourneyMatches(tournament);
			
			//Juego la fecha
			Services.Tournaments.TournamentsService.PlayNextJourney(tournament); // Error aquí: An attempt was made to remove a relationship between a Tournament and a Standing. However, one of the relationship's foreign keys (Standing.TournamentId) cannot be set to null

			Match[] nextJourneyMatches = Services.Tournaments.TournamentsService.GetTournamentNextJourneyMatches(tournament);
			Assert.AreEqual(firstJourneyMatches.Count(), nextJourneyMatches.Count());
		} 
	}
}
